package com.yy.ssjx;

import java.util.Properties;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;

/**
 * 描述:
 *
 * @author: yanglin
 * @Date: 2021-11-12-10:40
 * @Version: 1.0
 */
public class NamingExample {

    public static void main(String[] args) throws NacosException {

        Properties properties = new Properties();
        properties.setProperty("serverAddr", "139.224.187.212:13348");
        properties.setProperty("namespace", "85a8089c-4d4f-485d-b873-107e679528a6");

        NamingService naming = NamingFactory.createNamingService(properties);

        naming.registerInstance("seata-server", "139.224.187.212", 13392, "TEST1");

        System.out.println(naming.getAllInstances("seata-server"));

        naming.subscribe("seata-server", new EventListener() {
            @Override
            public void onEvent(Event event) {
                System.out.println(((NamingEvent)event).getServiceName());
                System.out.println(((NamingEvent)event).getInstances());
            }
        });
    }
}
